package com.leviassaf.androidbasics;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

public class ScaleMeasure {

	private static final String DATABASE_NAME = "ScaleMeasure";
	private static final int DATABASE_VERSION = 1;

	private DbHelper dbHelper;
	private final Context context;
	private SQLiteDatabase sqliteDatabase;

	public String strSQL;

	public class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub

		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			strSQL = "CREATE TABLE people "
					+ "(person_id INTEGER PRIMARY KEY AUTOINCREMENT, "
					+ "name TEXT NOT NULL," + "hotness TEXT NOT NULL);)";
			db.execSQL(strSQL);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			strSQL = "DROP TABLE IF EXISTS people;";
			onCreate(db);
		}

	}

	public ScaleMeasure(Context c) {
		context = c;
	}

	public ScaleMeasure open() throws SQLException {
		dbHelper = new DbHelper(context);
		sqliteDatabase = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public long insert(Person NewPerson){
		ContentValues contentValues = new ContentValues();
		contentValues.put("name", NewPerson.getName());
		contentValues.put("hotness", NewPerson.getHotness());
		return sqliteDatabase.insert("people", null, contentValues);
	}

	@SuppressWarnings("deprecation")
	public TableRow[] getData(String colName, Boolean isAscending) throws SQLException {
		// TODO Auto-generated method stub
		
		
		String[] arrStrColumns = new String[] { "person_id", "name", "hotness" };
		String strOrderBySort;
		if (isAscending){
			strOrderBySort = " ASC";
		}
		else {
			strOrderBySort = " DESC";
		}
		Cursor cursor = sqliteDatabase.query("people", arrStrColumns, null, null, null, null, colName + strOrderBySort);
		TableRow[] arrTableRow = new TableRow[cursor.getCount()];
		int intRowIndex = 0;
		while (cursor.moveToNext()){
			int intId = cursor.getInt(0);
			String strName = cursor.getString(1);
			String strScale = cursor.getString(2);
			TableRow tableRow = new TableRow(context);
			if (intRowIndex % 2 == 0){
				tableRow.setBackgroundColor(Color.BLACK);
			}
			else{
				tableRow.setBackgroundColor(Color.GRAY);
			}
			tableRow.setId(100 + intRowIndex);
			tableRow.setLayoutParams(
				new LayoutParams(
					LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT
				)
			);
			addTextViewToTableRow(tableRow, Integer.toString(intId), intRowIndex);
			addTextViewToTableRow(tableRow, strName, intRowIndex);
			addTextViewToTableRow(tableRow, strScale, intRowIndex);
			arrTableRow[intRowIndex] = tableRow;
			intRowIndex++;
		}
		return arrTableRow;
	}

	private void addTextViewToTableRow(TableRow Row, String ColValue, int RowIndex){
		TextView textViewName = new TextView(context);
		textViewName.setBackgroundResource(R.drawable.cell_shape);
		textViewName.setId(200 + RowIndex);
		textViewName.setText(ColValue);
		textViewName.setTextSize(20);
		textViewName.setGravity(Gravity.CENTER);
		textViewName.setPadding(2, 0, 5, 0);
		textViewName.setTextColor(Color.WHITE);
		Row.addView(textViewName);
	}

	public String getNameById(long l) throws SQLException {

		String[] columns = new String[] { "person_id", "name", "hotness" };
		Cursor c = sqliteDatabase.query("people", columns, "person_id" + "="
				+ l, null, null, null, null);
		if (c != null) {
			c.moveToFirst();
			String name = c.getString(c.getColumnIndex("name"));
			return name;
		}
		return null;
	}

	public String getHotnessById(long l) throws SQLException {
		String[] columns = new String[] { "person_id", "name", "hotness" };
		Cursor c = sqliteDatabase.query("people", columns, "person_id" + "="
				+ l, null, null, null, null);
		if (c != null) {
			c.moveToFirst();
			String scale = c.getString(c.getColumnIndex("hotness"));
			return scale;
		}
		return null;
	}

	public void delete(int ItemId) {
		// TODO Auto-generated method stub
		sqliteDatabase.delete("people", "person_id = " + ItemId, null);
	}
	
	public void update(Person editedPerson){
		ContentValues contentValues = new ContentValues();
		contentValues.put("name", editedPerson.getName());
		contentValues.put("hotness", editedPerson.getHotness());
		sqliteDatabase.update("people", contentValues, "person_id = " + editedPerson.getId(), null);
	}
}
